package servlet.DamServlets;

import dao.DamDAO;
import entity.Dam;
import entity.User;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * @author GGDog
 */
@WebServlet("/DoAddDamServlet")
public class DoAddDamServlet extends HttpServlet {
    DamDAO damDAO=new DamDAO();
    Dam dam;

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        doGet(req,resp);
    }

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String name=req.getParameter("name");
        String river=req.getParameter("river");
        int capacity= Integer.parseInt(req.getParameter("capacity"));
        String completeDate=req.getParameter("completeDate");

        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Date date;
        try {
            date=simpleDateFormat.parse(completeDate);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        dam=new Dam(name,river,capacity,date);
        damDAO.insert(dam);

        ArrayList<Dam> damList = damDAO.findAll();

        req.setAttribute("damList", damList);

        RequestDispatcher dispatcher = req.getRequestDispatcher("/DamList.jsp");
        dispatcher.forward(req, resp);
    }

}
